/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dawnland03.data.map.algorithm.astar;

import dawnland03.data.map.model.MatrixCell;

/**
 * @author Petru Obreja (obrejap@yahoo.com)
 */
class AlgorithmCell implements Comparable {
    private final MatrixCell node;
    private double gScore = 0; //Distance from start along optimal path.
    private double hScore = 0; //heuristic_estimate_of_distance(current node, goal)
    private AlgorithmCell previousCellInPath = null;

    public AlgorithmCell(MatrixCell node) {
        this.node = node;
    }

    public MatrixCell getNode() {
        return node;
    }

    public double getGScore() {
        return gScore;
    }

    public void setGScore(double gScore) {
        this.gScore = gScore;
    }

    public double getHScore() {
        return hScore;
    }

    public void setHScore(double hScore) {
        this.hScore = hScore;
    }

    public double getFScore() {
        return gScore+hScore;
    }

    public AlgorithmCell getPreviousCellInPath() {
        return previousCellInPath;
    }

    public void setPreviousCellInPath(AlgorithmCell previousCellInPath) {
        this.previousCellInPath = previousCellInPath;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        AlgorithmCell that = (AlgorithmCell) o;

        if (node != null ? !node.equals(that.node) : that.node != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return (node != null ? node.hashCode() : 0);
    }

    @Override
    public int compareTo(Object o) {
        return (int) (getFScore() - ((AlgorithmCell) o).getFScore());
    }

    @Override
    public String toString() {
       return String.format("{%1$2d %2$.2f %3$.2f %4$.2f}", node.getId(), gScore, hScore, getFScore());
    }
}
